package ui.recipe
{
	import events.GestureEvent;
	
	import flash.display.Sprite;
	import flash.geom.Point;
	
	import global.Managers;
	
	import interactive.HandType;
	
	import resources.Resources;
	
	import ui.components.ebook.EBook;
	import ui.faces.DiningFace;
	import ui.layout.IRecipeDetail;
	import ui.layout.Page;
	import ui.panels.PanelType;
	import ui.panels.RecipeDetailPanel;
	
	import utils.pool.ObjectPool;
	
	import vo.layout.LayoutVO;
	import vo.layout.PageVO;

	/**
	 *
	 * 该类用在DiningFace中，用来播放电子菜谱 
	 * @author Raykid
	 * 
	 */
	public class Recipe extends Sprite
	{
		public static const RECIPE_WIDTH		: Number = 800;
		public static const RECIPE_HEIGHT		: Number = 700;
		
		private var _ebook						: EBook;
		private var _lastX						: Number;
		private var _lastY						: Number;
		
		private var _chooseTarget				: IRecipeDetail = null;
		
		public function Recipe()
		{
			super();
			init();
		}
		private function init():void {
			_ebook = new EBook(RECIPE_WIDTH, RECIPE_HEIGHT);
			addChild(_ebook);
			// 初始化事件
			var face:DiningFace = Managers.faceManager.diningFace;
			face.addEventListener(GestureEvent.CLICK, clickHandler);
			face.addEventListener(GestureEvent.LEAVE, leaveHandler);
			face.addEventListener(GestureEvent.DOUBLE_CLICK, doubleClickHandler);
			face.addEventListener(GestureEvent.TOUCH, touchHandler);
			face.addEventListener(GestureEvent.MOVE, moveHandler);
			face.addEventListener(GestureEvent.HAND, handHandler);
		}
		private function clickHandler(event:GestureEvent):void {
			// 如果是可以弹出详细菜单界面的对象则进行记录
			var target:IRecipeDetail = event.gestureTarget as IRecipeDetail;
			if(target != null) {
				_chooseTarget = target;
			}
		}
		private function leaveHandler(event:GestureEvent):void {
			if(_chooseTarget != null) {
				// 打开菜单详细界面
				var panel:RecipeDetailPanel = Managers.panelManager.getPanel(PanelType.RECIPE_DETAIL) as RecipeDetailPanel;
				Managers.panelManager.open(PanelType.RECIPE_DETAIL);
				panel.update(Managers.dishManager.getValueByID(_chooseTarget.dishID), _chooseTarget.priceUnitName);
				_chooseTarget = null;
			}
		}
		private function doubleClickHandler(event:GestureEvent):void {
			_chooseTarget = null;
			if(_ebook.zoomState) {
				_ebook.zoomOut();
			} else {
				var pt:Point = new Point(event.stageX, event.stageY);
				pt = _ebook.globalToLocal(pt);
				_ebook.zoomIn(pt.x, pt.y);
			}
		}
		private function touchHandler(event:GestureEvent):void {
			_lastX = event.stageX;
			_lastY = event.stageY;
		}
		private function moveHandler(event:GestureEvent):void {
			var x:Number = event.stageX - _lastX;
			var y:Number = event.stageY - _lastY;
			_ebook.move(x, y);
			_lastX = event.stageX;
			_lastY = event.stageY;
		}
		private function handHandler(event:GestureEvent):void {
			switch(event.handType) {
				case HandType.HAND_LEFT:
					_ebook.nextPage();
					break;
				case HandType.HAND_RIGHT:
					_ebook.prevPage();
					break;
			}
		}
		
		public function update(dishes:Array, layoutVO:LayoutVO):void {
			var pages:Array = [];
			for each(var pageVO:PageVO in layoutVO.pages) {
				var page:Page = ObjectPool.pop(Page) as Page;
				page.update(pageVO);
				pages.push(page);
			}
			pages.unshift(Managers.resourceManager.getBitmap(Resources.RECIPE_COVER_FRONT));
			pages.push(Managers.resourceManager.getBitmap(Resources.RECIPE_COVER_BACK));
			_ebook.update(pages);
		}
		public function reset():void {
			_ebook.reset();
		}
	}
}